//
//  TempListExt.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/12/30.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit

extension KSTempListVC {
    func addAction(_ scanResult:String = "") {
        
        // web形式的新增
        if  self.rootTemp.webFormUrl != "" {
            let cwebV = SepWebVC()
            cwebV.refreshClosure = {
                self.tableView.mj_header?.beginRefreshing()
            }
            let uurl =  getUrl4ziding(str: self.rootTemp.webFormUrl, mainDic: [:])
            cwebV.reportUrl = uurl
            KSLog(uurl)
            cwebV.title = self.rootTemp.menu_nm
            self.navigationController?.pushViewController(cwebV, animated: true)
            
            
        } else {
            // 自主表单的新增
            let view  = ConfigMenuForm(rootTemp:rootTemp,templateHandle: templateHandle, templateDic: templateDic)
            view.listScanResult = scanResult
            view.refreshClosure = {
                self.tableView.mj_header?.beginRefreshing()
            }
            self.navigationController?.pushViewController(view, animated: true)
        }
     
        
    }
}
// 跳转配置菜单
// 跳转到其他表单   菜单名   参数   one是根据配置按需给新表单的getform传参数值  straightDic 直接给表单传所有字典
// backto 完成后回到哪个页面
//  oneXX 示例： 生成报废单:z_one_no=${z_one_no}&z_zzda_id=${z_zzda_id}:0:form:费用报销

// 匹配菜单 配置那边自定义跳转用中文匹配，渔美康那边审批流程用id匹配
func findTempMenu(menuName:String) -> MainTempModel? {
    var selectedRootTemp:MainTempModel?
    KSLog("开始查找菜单-\(menuName)\n")
    // outer就是当内层循环中 符合条件 这个双层循环完全结束。 (找到菜单)
outer:for i in 0..<cachedDynamicArray.count {
    KSLog("目录-" + cachedDynamicArray[i].0)
    for onex in cachedDynamicArray[i].1 {
        KSLog("000-" + onex.menu_nm.unwrap)
        // 包含子菜单，
        if onex.tempArr.count > 0 {
            for chOnex in onex.tempArr {
                KSLog("子目录找-" + chOnex.menu_nm.unwrap)
                if chOnex.menu_nm == menuName {
                    selectedRootTemp = chOnex
                    break outer
                }
            }
        } else {
            KSLog("找-" + onex.menu_nm.unwrap)
            if onex.menu_nm == menuName {
                selectedRootTemp = onex
                break outer
            }
            
        }
    }
}
    return selectedRootTemp
}
func findTempMenu(menuId:String) -> MainTempModel? {
    var selectedRootTemp:MainTempModel?
    KSLog("开始查找菜单-\(menuId)\n")
    // outer就是当内层循环中 符合条件 这个双层循环完全结束。 (找到菜单)
outer:for i in 0..<cachedDynamicArray.count {
    KSLog("目录-" + cachedDynamicArray[i].0)
    for onex in cachedDynamicArray[i].1 {
        KSLog("000-" + onex.id_key.unwrap)
        // 包含子菜单，
        if onex.tempArr.count > 0 {
            for chOnex in onex.tempArr {
                KSLog("子目录找-" + chOnex.id_key.unwrap)
                if chOnex.id_key == menuId {
                    selectedRootTemp = chOnex
                    break outer
                }
            }
        } else {
            KSLog("找-" + onex.id_key.unwrap)
            if onex.id_key == menuId {
                selectedRootTemp = onex
                break outer
            }
            
        }
    }
}
    return selectedRootTemp
}
func go2SpeMenuList(vc:UIViewController,menuName:String) {
    let selectedRootTemp = findTempMenu(menuName: menuName)
    if selectedRootTemp.isSome {
        
        go2TempUtil(pvc: vc, rtemp: selectedRootTemp!)

        
    } else {
        KSToast("未找到跳转菜单-\(menuName)")
    }
    
}


/// 跳转配置菜单页面
/// - Parameters:
///   - oneXX: 第二个元素是取值公式; 第五个元素是配置菜单的 id 或名称
///   - one: 填入菜单的数据
///   - straightDic:
///   - vc: push 的 vc
///   - backto:
///   - moduleID:
///   - excuteType: 单据的类型(新增,查看,修改等等)
///   - auditParamCombine:
func go2SpeMenuForm(oneXX:[String],one:[String:Any],straightDic:[String:Any],vc:UIViewController,backto:UIViewController.Type? = nil,moduleID:String  = "",excuteType:OperateType = .add,auditParamCombine:AuditParamCombine? = nil) {
    guard oneXX.count == 5 else {
        KSToast("form类型自定义按钮配置错误")
        return
    }
    let menuName = oneXX[4]
    let paramStr = oneXX[1]
    var selectedRootTemp:MainTempModel?
    if let findMwithId = findTempMenu(menuId: menuName) {
        selectedRootTemp = findMwithId
    }
    if let findMwithNm = findTempMenu(menuName: menuName) {
        selectedRootTemp = findMwithNm
    }
    
    if selectedRootTemp.isSome {
        
        fetchTempData(menuid: selectedRootTemp!.id_key.unwrap) { templateHandle, templateDic in
            let view  = ConfigMenuForm(rootTemp:selectedRootTemp!,templateHandle: templateHandle, templateDic: templateDic)
            
            
            if excuteType == .auditing || excuteType == .audited {
                TempHelper.shared.api_WflowTaskSet(auditParamCombine: auditParamCombine!) { ss in
                    getProcessInfo(chooseUsers: ss)
                }
         
                func getProcessInfo(chooseUsers:[ChooseUsers]) {
                    
                    TempHelper.shared.api_progressTaskInfo(auditParamCombine: auditParamCombine) { resultinfo in
                        var info = resultinfo
                        if let newflow = info.newflow {
                            info.progress = newflow.progress
                        }
                        let paArr = paramStr.components(separatedBy: "&")
                        var newD = [String:String]()
                        for item in paArr {
                            let preFrom =  item.preChar(char: "=").unwrap
                            let preSuf =  item.sufChar(char: "=").unwrap
                            let rKey = preSuf.removeBrackets.removeDolendAt.replacingOccurrences(of: "a.", with: "")
                            newD.updateValue(any2Str(one[rKey]), forKey: preFrom)
                        }
                        view.excuteType = excuteType
                        view.backto = backto
                        view.fromDifUrlParaDic = newD
                        view.fromDifStraightDic = straightDic
                        if auditParamCombine.isSome {
                            var newA = auditParamCombine!
                            newA.info = info
                            newA.chooseUser = chooseUsers
                            view.tempAuditParamC = newA
                        }
                        view.refreshClosure = {
                            if let tempVc = vc as? KSTempListVC {
                                tempVc.tableView.mj_header?.beginRefreshing()
                            }
                            
                        }
                        vc.navigationController?.pushViewController(view, animated: true)
                        
                    }
                    
 
                }
                

            } else {
                let paArr = paramStr.components(separatedBy: "&")
                var newD = [String:String]()
                for item in paArr {
                    let preFrom =  item.preChar(char: "=").unwrap
                    let preSuf =  item.sufChar(char: "=").unwrap
                    let rKey = preSuf.removeBrackets.removeDolendAt.replacingOccurrences(of: "a.", with: "")
                    newD.updateValue(any2Str(one[rKey]), forKey: preFrom)
                }
                view.excuteType = excuteType
                view.backto = backto
                view.fromDifUrlParaDic = newD
                view.fromDifStraightDic = straightDic
                if auditParamCombine.isSome {
                    view.tempAuditParamC = auditParamCombine!
                }
                view.refreshClosure = {
                    if let tempVc = vc as? KSTempListVC {
                        tempVc.tableView.mj_header?.beginRefreshing()
                    }
                    
                }
                vc.navigationController?.pushViewController(view, animated: true)
            }
            
            
            
        }
        
        
    } else {
        KSToast("未找到跳转菜单-\(menuName)")
    }
    
    
}
